﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.ObjectModel;

/// <summary>
/// Summary description for HtmlTag
/// </summary>
public class HtmlContainer : HtmlTag, IHtmlObject
{
    private List<IHtmlObject> children;
	public HtmlContainer(string newTag) : base(newTag)
	{
        children = new List<IHtmlObject>();
	}

    public void AddChild(IHtmlObject newChild)
    {
        children.Add(newChild);
    }

    public void AddChild(string newText)
    {
        children.Add(new HtmlText(newText));
    }

    public void RemoveChild(int index)
    {
        children.RemoveAt(index);
    }

    public void RemoveChild(IHtmlObject child)
    {
        children.Remove(child);
    }

    public List<IHtmlObject> GetChildren
    {
        get
        {
            return children;
        }
        set 
        {
            children = value;
        }
    }

    public string ToHtml()
    {
        string tempAttr="";
        string tempChildren ="";
        foreach (string sValue in GetAttributes.Keys)
        {
            tempAttr += sValue + "=";
            foreach (string sAttr in GetAttributes[sValue])
            {
                tempAttr += sAttr + ",";
            }
            if (tempAttr[tempAttr.Length - 1] == ',')
            {
                tempAttr.Remove(tempAttr.Length - 1);
            }
        }
        foreach (IHtmlObject h in children)
        {
            tempChildren += h.ToHtml();
        }
        return String.Format("<{0} {1}>{2}</{0}>", Tag, tempAttr, tempChildren);
    }
}